package io.lvdaxian.handwriting.concurrency;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConcurrencyBucketTests {
  
  // 表示回调的consumer 表达式
  private static Consumer<List<String>> consumerCallback = (excludeLists) -> {
    if (excludeLists.isEmpty()) {
      return;
    }
    
    excludeLists.forEach(l -> {
      System.out.printf("用户: %s 被挤掉了%n", l);
    });
  };
  
  @Test
  public void test01() {
    ConcurrencyBucket instance = ConcurrencyBucket.getInstance();
    instance.initializationBucket(5);
    
    for (int idx = 0; idx < 2; idx++) {
      String user = "a" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    
    for (int idx = 0; idx < 5; idx++) {
      String user = "b" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    
    // -- 剔除逻辑
    instance.reMergeConcurrencyBucket(1, "a1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    // -- 扩容
    System.out.println("-- 扩容逻辑 --");
    instance.reMergeConcurrencyBucket(10, "a1", consumerCallback);
    // -- 添加元素
    for (int idx = 0; idx < 5; idx++) {
      String user = "c" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
    
    // -- 剔除逻辑
    instance.reMergeConcurrencyBucket(2, "c1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    // -- 剔除逻辑
    instance.reMergeConcurrencyBucket(1, "a1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    // -- 扩容
    System.out.println("-- 扩容逻辑 --");
    instance.reMergeConcurrencyBucket(2, "a1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    // -- 添加元素
    for (int idx = 0; idx < 3; idx++) {
      String user = "d" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
  }
  
  @Test
  public void test02() {
    ConcurrencyBucket instance = ConcurrencyBucket.getInstance();
    instance.initializationBucket(2);
    
    for (int idx = 0; idx < 2; idx++) {
      String user = "a" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- exit");
    instance.exitConcurrencyBucket("a1");
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- exit");
    instance.exitConcurrencyBucket("a0");
    System.out.println(Arrays.toString(instance.getBucket()));
    
    for (int idx = 0; idx < 2; idx++) {
      String user = "b" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
    
    instance.reMergeConcurrencyBucket(1, "b1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    instance.reMergeConcurrencyBucket(2, "b1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    for (int idx = 0; idx < 1; idx++) {
      String user = "c" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 扩容");
    instance.reMergeConcurrencyBucket(10, "b1", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("--退出登录");
    instance.exitConcurrencyBucket("b1");
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 缩容");
    instance.reMergeConcurrencyBucket(1, "c0", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 登录");
    for (int idx = 0; idx < 1; idx++) {
      String user = "d" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 扩容");
    instance.reMergeConcurrencyBucket(2, "c0", consumerCallback);
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 登录");
    for (int idx = 0; idx < 1; idx++) {
      String user = "e" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 退出登录");
    instance.exitConcurrencyBucket("c0");
    System.out.println(Arrays.toString(instance.getBucket()));
    
    System.out.println("-- 登录");
    for (int idx = 0; idx < 1; idx++) {
      String user = "f" + idx;
      boolean status = instance.joinConcurrencyBucket(user);
      
      System.out.printf("用户: %s, 加入状态: %s%n", user, status);
    }
    System.out.println(Arrays.toString(instance.getBucket()));
  }
}
